package Scnen3;

import Picture.GameImage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.net.URL;

public class PlantPanel2 extends JPanel {

    public void reSize(Frame frame){
        frame.setSize(1945,1270);
        validate();
        repaint();
    }





    public  int flag = 0;
    J j = new J();

    Scene3 scene3 = new Scene3();
    boolean Onpic = false;
    boolean Onpico2 = false;
    boolean Onpich2o = false;
    boolean Onpicglucose = false;
    bg bg;

    CO2 co2;
    int begin_x;
    int begin_y;


    O2 o2;
    int begin_xo2;
    int begin_yo2;

    H2O h2o;
    int begin_xh2o;
    int begin_yh2o;

    glucose glucose;
    int begin_xglucose;
    int begin_yglucose;

    int lianF = 0;
    public  int stopF = 0;
    public PlantPanel2(Frame frame){


        co2 = new CO2();
        co2.x = 1500;
        co2.y = 1000;


        o2 = new O2();
        o2.x = 1200;
        o2.y = 1000;


        h2o = new H2O();
        h2o.x = 500;
        h2o.y = 1000;




        glucose = new glucose();
        glucose.x = 200;
        glucose.y = 950;


        bg = new bg();
        this.add(bg);
        setLayout(null);
        bg.setLocation(0,0);



        new Thread(()->{
            while(true){
                sun.changeImage();
                j.changeImage();
                repaint();
                if(judge()&&sun.y<900){
                    lianF = 1;
                    sun.y+=20;
                }
                if(sun.y>=900){
                    stopF+=10;
                }
                try {
                    Thread.sleep(90);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }).start();


        addMouseListener(new MouseAdapter() {
           public void mousePressed(MouseEvent e){
               if(isOnPic(e.getX(),e.getY())){
                   begin_x = e.getX();
                   begin_y = e.getY();
                   Onpic = true;
               }

               if(isOnPico2(e.getX(),e.getY())){
                   begin_xo2 = e.getX();
                   begin_yo2 = e.getY();
                   Onpico2 = true;
                   repaint();
               }

               if(isOnPich2o(e.getX(),e.getY())){
                   begin_xh2o = e.getX();
                   begin_yh2o = e.getY();
                   Onpich2o = true;
                   repaint();
               }

               if(isOnPicglucose(e.getX(),e.getY())){
                   begin_xglucose = e.getX();
                   begin_yglucose = e.getY();
                   Onpicglucose = true;
               }
           }




            @Override
            public void mouseReleased(MouseEvent e) {
                Onpic = false;
                Onpico2 = false;
                Onpich2o = false;
                Onpicglucose = false;
            }
        });
        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                if(Onpic&&check(e.getX(),e.getY())){
                    co2.x = co2.x-(begin_x-e.getX());
                    co2.y = co2.y-(begin_y-e.getY());
                    begin_x = e.getX();
                    begin_y = e.getY();
                    repaint();

                }

                if(Onpico2&&check(e.getX(),e.getY())){
                    o2.x = o2.x-(begin_xo2-e.getX());
                    o2.y = o2.y-(begin_yo2-e.getY());
                    begin_xo2 = e.getX();
                    begin_yo2 = e.getY();
                    repaint();
                }
                if(Onpich2o&&check(e.getX(),e.getY())){
                    h2o.x = h2o.x-(begin_xh2o-e.getX());
                    h2o.y = h2o.y-(begin_yh2o-e.getY());
                    begin_xh2o = e.getX();
                    begin_yh2o = e.getY();
                    repaint();

                }

                if(Onpicglucose&&check(e.getX(),e.getY())){
                    glucose.x = glucose.x-(begin_xglucose-e.getX());
                    glucose.y = glucose.y-(begin_yglucose-e.getY());
                    begin_xglucose = e.getX();
                    begin_yglucose = e.getY();
                    repaint();
                }
            }
        });
    }
    public boolean judge(){
        return (judgeCO2(co2)&&judgeH2O(h2o)&&judgeGlucose(glucose)&&judgeO2(o2));
    }
    private boolean judgeO2(O2 o2) {
        if(o2.x>900&&o2.y>900)
        return true;
        else
        return false;
    }
    private boolean judgeGlucose(Scnen3.glucose glucose) {
        if(glucose.x>900&&glucose.y>900)
        return true;
        else
        return false;
    }

    private boolean judgeH2O(H2O h2o) {
        if(h2o.x<700&&h2o.y>900)
        return true;
        else
        return false;
    }

    private boolean judgeCO2(CO2 co2) {
        if(co2.x<700&&co2.y>900)
        return true;
        else
        return false;
    }


    private boolean check(int x, int y) {
        if(x<0||y<0){
            return false;

        }
        if(x>getWidth()||y>getHeight()){
            return false;
        }
        return true;
    }

    private boolean isOnPic(int x, int y) {
        if(x>=co2.x&&x<=co2.x+co2.img.getWidth()&&y>=co2.y&&y<=co2.y+co2.img.getHeight()){
            return true;
        }
        return false;
    }

    private boolean isOnPico2(int x, int y) {
        if(x>=o2.x&&x<=o2.x+o2.img.getWidth()&&y>=o2.y&&y<=o2.y+o2.img.getHeight()){
            return true;
        }
        return false;
    }

    private boolean isOnPich2o(int x, int y) {
        if(x>=h2o.x&&x<=h2o.x+h2o.img.getWidth()&&y>=h2o.y&&y<=h2o.y+h2o.img.getHeight()){
            return true;
        }
        return false;
    }

    private boolean isOnPicglucose(int x, int y) {
        if(x>=glucose.x&&x<=glucose.x+glucose.img.getWidth()&&y>=glucose.y&&y<=glucose.y+glucose.img.getHeight()){
            return true;
        }
        return false;
    }
    Sun sun = new Sun();

    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(j.img,800,1000,null);
        g.drawImage(glucose.img,glucose.x,glucose.y,null);
        g.drawImage(o2.img,o2.x,o2.y,null);
        g.drawImage(co2.img,co2.x,co2.y,null);
        g.drawImage(h2o.img,h2o.x,h2o.y,null);
        g.drawImage(sun.img,sun.x,sun.y,null);
        if(lianF == 0)
        g.drawImage(scene3.map.get("链子"),800,900,null);
        Font font = new Font(Font.SERIF, Font.PLAIN, 50);
        g.setFont(font);
        if(flag==0) {
            g.drawImage(scene3.map.get("提示框"), 700, 200, null);
            g.drawImage(scene3.map.get("1"),920,450,null);
            g.drawImage(scene3.map.get("2"),850,550,null);
            g.drawImage(scene3.map.get("3"),940,750,null);
            g.drawImage(scene3.map.get("4"),750,850,null);
            g.drawImage(scene3.map.get("log"),800,400,null);
            g.drawImage(scene3.map.get("log"),800,700,null);
            g.drawImage(scene3.map.get("button"),900,1030,null);
            g.drawImage(scene3.map.get("begin"),910,1040,null);
            g.drawImage(scene3.map.get("dec1"),700,350,null);
            g.drawImage(scene3.map.get("dec2"),700,400,null);
            g.drawImage(scene3.map.get("dec1"),700,500,null);
            g.drawImage(scene3.map.get("dec2"),700,600,null);
            g.drawImage(scene3.map.get("dec1"),700,650,null);
            g.drawImage(scene3.map.get("dec2"),700,800,null);
            g.drawImage(scene3.map.get("dec1"),700,800,null);
            g.drawImage(scene3.map.get("dec1"),700,950,null);
            g.drawImage(scene3.map.get("dec2"),700,1000,null);


            g.drawImage(scene3.map.get("dec1-"),1280,350,null);
            g.drawImage(scene3.map.get("dec2-"),1280,400,null);
            g.drawImage(scene3.map.get("dec1-"),1280,500,null);
            g.drawImage(scene3.map.get("dec2-"),1280,600,null);
            g.drawImage(scene3.map.get("dec1-"),1280,650,null);
            g.drawImage(scene3.map.get("dec2-"),1280,800,null);
            g.drawImage(scene3.map.get("dec1-"),1280,800,null);
            g.drawImage(scene3.map.get("dec1-"),1280,950,null);
            g.drawImage(scene3.map.get("dec2-"),1280,1000,null);
        }
        g.drawImage(scene3.map.get("按钮"),1770,150,null);
        g.drawString("合成",1800,200);


    }

}
